package com.jabo.douban.demo.presentation.view.fragment;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.jabo.douban.demo.R;
import com.jabo.douban.demo.presentation.viewmodel.SearchViewModel;

public class SearchFragment extends Fragment {

    private SearchViewModel mViewModel;
    private FrameLayout mSearchFrameLayout;

    public static SearchFragment newInstance() {
        return new SearchFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_fragment, container, false);
        initView(view);
        return view;
    }

    private void initView(final View view) {
        mSearchFrameLayout = view.findViewById(R.id.search_edit_frame);
        view.findViewById(R.id.ivMessage).setVisibility(View.GONE);
        view.findViewById(R.id.ivScan).setVisibility(View.GONE);
        view.findViewById(R.id.tvCancel).setVisibility(View.VISIBLE);

        view.post(new Runnable() {
            @Override
            public void run() {
                final int width = mSearchFrameLayout.getWidth();
                final ValueAnimator valueAnimator = ValueAnimator.ofInt(width, width + 80);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        mSearchFrameLayout.getLayoutParams().width = (int) animation.getAnimatedValue();
                        mSearchFrameLayout.requestLayout();
                        Log.e("zjb", "onAnimationUpdate: " + mSearchFrameLayout.getLayoutParams().width);
                    }
                });
                valueAnimator.setDuration(600);
                valueAnimator.start();
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(SearchViewModel.class);
    }

}
